package com.umfb.bootstrap.commons;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接受datatables发送的分页参数 - -！ 尼玛～ 好难
 * Created by zcah on 18-5-21.
 */
public class SentParameters {


    /**
     * 绘制计数器。这个是用来确保Ajax从服务器返回的是对应的（Ajax是异步的，因此返回的顺序是不确定的）。 要求在服务器接收到此参数后再返回
     */
    private int draw;

    /**
     * 第一条数据的起始位置， 比如0代表第一条数据
     */
    private int start;

    /**
     * 告诉服务器每页显示的条数， 这个数字会等于返回的 data集合的记录数，可能会大于因为服务器可能没有那么多数据。这个也可能是-1，
     * 代表需要返回全部数据
     */
    private int length;

    /**
     * 全局的搜索条件，
     */
    private Search search;

    /**
     * 告诉后台那些列是需要排序的。i是一个数组索引
     */
    private List<Order> order;

    /**
     * 定了这个表格里所有的列
     *
     */
    private List<Column> columns;

    public SentParameters() {
        this.start = 0;
        this.length = 10;
        this.search = new Search();
        this.order = new ArrayList<Order>();
        this.columns = new ArrayList<Column>();
    }

    public int getDraw() {
        return draw;
    }

    public void setDraw(int draw) {
        this.draw = draw;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public List<Order> getOrder() {
        return order;
    }

    public void setOrder(List<Order> order) {
        this.order = order;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }

    /**
     *
     * @return a {@link Map} of {@link} indexed by name
     */
    public Map<String, Column> getColumnsAsMap() {
        Map<String, Column> map = new HashMap<String, Column>();
        for (Column column : columns) {
            map.put(column.getData(), column);
        }
        return map;
    }

    /**
     * Find a column by its name
     *
     * @param columnName
     *            the name of the column
     * @return the given Column, or <code>null</code> if not found
     */
    public Column getColumn(String columnName) {
        if (columnName == null) {
            return null;
        }
        for (Column column : columns) {
            if (columnName.equals(column.getData())) {
                return column;
            }
        }
        return null;
    }

    /**
     * Add a new column
     *
     * @param columnName
     *            the name of the column
     * @param searchable
     *            whether the column is searchable or not
     * @param orderable
     *            whether the column is orderable or not
     * @param searchValue
     *            if any, the search value to apply
     */
    public void addColumn(String columnName, boolean searchable, boolean orderable, String searchValue) {
        this.columns.add(new Column(columnName, "", searchable, orderable, new Search(searchValue, false)));
    }

    /**
     * Add an order on the given column
     *
     * @param columnName
     *            the name of the column
     * @param ascending
     *            whether the sorting is ascending or descending
     */
    public void addOrder(String columnName, boolean ascending) {
        if (columnName == null) {
            return;
        }
        for (int i = 0; i < columns.size(); i++) {
            if (!columnName.equals(columns.get(i).getData())) {
                continue;
            }
            order.add(new Order(i, ascending ? "asc" : "desc"));
        }
    }


    public int getPage(){
        return this.start/this.length+1;
    }

    @Override
    public String toString() {
        return "DataTablesInput [draw=" + draw + ", start=" + start + ", length=" + length + ", search=" + search
                + ", order=" + order + ", columns=" + columns + "]";
    }
}
